home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_12 / guthrie2 / demodate.c < prev    next >
C/C++ Source or Header  |  1994-01-22  |  3KB  |  99 lines

  1. /* DEMODATE.C - Demonstration application for Date Formatting. */
  2. /* Written by: R. Scott Guthrie                                */
  3. /* Requires XLATE functions and translate file entries for     */
  4. /*  "Month Position", "Day Position", "Year Position", and     */
  5. /*  "Date Format".                                             */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "xlate.h"
  11.  
  12. /**** DateFormatter *********************************************/
  13. /* Function to build date string based on XLATE file definition */
  14. /* using passed in date values.  Returns the date string.       */
  15. /* Requires translate file line entries of:                     */
  16. /*  "Month Position", "Day Position", "Year Position", and      */
  17. /*  "Date Format".                                              */
  18. /****************************************************************/
  19. void DateFormatter(char *Date, int Day, int Month, int Year)
  20. {
  21.   char order[3];    /* Holds the display order of M, D, Y */
  22.   int  value[3];    /* Holds the date value in desired order */
  23.   char format[15];  /* Holds the date format string */
  24.   int  i;           /* index variable */
  25.   int  temp;        /* temporary integer */
  26.  
  27.   /* Get the Display Order information */
  28.   order[atoi(Xlate("Day Position"))]   = 'D';
  29.   order[atoi(Xlate("Month Position"))] = 'M';
  30.   order[atoi(Xlate("Year Position"))]  = 'Y';
  31.  
  32.   /* Get the Format Definition */
  33.   strcpy(format, Xlate("Date Format"));
  34.  
  35.   /* Establish date component order */
  36.   for(i=0; i<3; i++)
  37.   {
  38.     switch(order[i])
  39.     {
  40.       case 'M':
  41.         temp = Month;
  42.         break;
  43.       case 'D':
  44.         temp = Day;
  45.         break;
  46.       case 'Y':
  47.         temp = Year;
  48.         break;
  49.       default :
  50.         break;
  51.     }
  52.     value[i] = temp;  /* store date values in desired order */
  53.   }
  54.  
  55.   /* Create the date string */
  56.   sprintf(Date, format, value[0], value[1], value[2]);
  57.  
  58.   return;
  59. }
  60.  
  61. void main()
  62. {
  63.   int iDay = 23;      /* Define a DAY value */
  64.   int iMonth = 4;     /* Define a Month value */
  65.   int iYear = 93;     /* Define a YEAR value */
  66.   char date[12];      /* Formatted date is placed in this string */
  67.  
  68.   /* Show current date values */
  69.   printf("Given a DAY value of %d, ", iDay);
  70.   printf("a MONTH value of %d, ", iMonth);
  71.   printf("and a YEAR value of %d:\n", iYear);
  72.  
  73.   /* Establish AMERICAN date format (as described in AMERICAN.TRN) */
  74.   XlateSet("AMERICAN");
  75.  
  76.   /* Format the Date String according to XLATE file information */
  77.   DateFormatter(date, iDay, iMonth, iYear);
  78.   printf("%s is America's Format.\n", date);
  79.  
  80.   /* Establish ENGLISH date format (as described in ENGLAND.TRN) */
  81.   /* Note: 'XlateSet()' removes the existing table */
  82.   XlateSet("ENGLAND");
  83.  
  84.   /* Print the Date String formatted by 'DateFormatter' */
  85.   DateFormatter(date, iDay, iMonth, iYear);
  86.   printf("%s is England's Format.\n", date);
  87.  
  88.   /* Establish an alternate date format (as described in ALTDATE.TRN) */
  89.   XlateSet("ALTDATE");
  90.  
  91.   /* Print the Date String formatted by 'DateFormatter' */
  92.   DateFormatter(date, iDay, iMonth, iYear);
  93.   printf("%s as an Alternate Format.\n", date);
  94.  
  95.   /* Free ALTDATE Translate Table memory */
  96.   XlateFree();
  97. }
  98. /* end source file DEMODATE.C */
  99.